package com.ulwx.database;



import com.ulwx.database.DataBase.MainSlaveModeConnectMode;
import com.ulwx.database.dbpool.DBPoolFactory;
import com.ulwx.tool.DbUtils;

public class DataBaseFactory {


	
	/**
	 * 用于从连接池构造DataBase对象，连接池配置文件在src目录下的dbpool.xml里
	 * @return
	 * @throws DbException
	 */
	public static DataBase getDataBase() throws DbException {
		return getDataBase(MainSlaveModeConnectMode.Try_Connect_MainServer);

	}
	
	public static DataBase getDataBase(MainSlaveModeConnectMode mainSlaveModeConnectMode) throws DbException {
		DataBase db = new DataBase();
		try {
			db.connectDb("tt1",mainSlaveModeConnectMode);
//			 db.connectDb("gameuser", "jb98",
//		 "jdbc:mysql://localhost:10001/versiondb",
//			 "com.mysql.jdbc.Driver");
		} catch (DbException e) {
			throw new DbException("can't connect database!", e);
		}
		return db;

	}
	/**
	 * 
	 * @param dataSourceName 连接池的名字
	 * @return
	 * @throws DbException
	 */
	public static DataBase getDataBase(String dataSourceName) throws DbException {
		return getDataBase(dataSourceName,MainSlaveModeConnectMode.Try_Connect_MainServer);

	}
	
	public static DataBase getDataBase(String dataSourceName,MainSlaveModeConnectMode mainSlaveModeConnectMode ) throws DbException {
		DataBase db = new DataBase();
		try {
			db.connectDb(dataSourceName,mainSlaveModeConnectMode);
//			 db.connectDb("gameuser", "jb98",
//		 "jdbc:mysql://localhost:10001/versiondb",
//			 "com.mysql.jdbc.Driver");
		} catch (DbException e) {
			throw new DbException("can't connect database!", e);
		}
		return db;

	}
	/**
	 * @param args
	 * @throws
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			System.out.println("start..................");
			DBPoolFactory.init();
			System.out.println("init..................");
			
			//System.out.println("sleep out..................");
			
			String sql="select * from BasePlatform";
			DataBaseSet rs=DbUtils.doCachedQuery(null,sql, null);
			while(rs.next()){
				System.out.println(rs.getDateTime("UpdateTime"));
			}
			
			 rs=DbUtils.doCachedQuery(null,sql, null);
			while(rs.next()){
				System.out.println(rs.getDateTime("UpdateTime"));
			}
			 rs=DbUtils.doCachedQuery(null,sql, null);
		
			//Thread.sleep(10000000);
			DBPoolFactory.getInstance().clearAll();
			

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
